package com.alibaba.travel.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.travel.common.utils.RRException;
import com.alibaba.travel.search.constant.EsConstant;
import com.alibaba.travel.search.model.dto.ArticleSearchRequest;
import com.alibaba.travel.search.entity.EsArticle;
import com.alibaba.travel.search.feign.ArticleFeignService;
import com.alibaba.travel.search.service.ArticleSearchService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: where
 * @Date: 2023/03/09/8:36
 * @Description:
 */
@Service
public class ArticleSearchServiceImpl implements ArticleSearchService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    private ArticleFeignService articleFeignService;




    @Override
    public void upload(Long[] ids) {
        // openfeign 远程调用查询文章信息
        List<EsArticle> esArticleList = articleFeignService.getDetails(ids).getData("list", new TypeReference<List<EsArticle>>() {});
        upload(esArticleList);
    }

    @Override
    public void upload(List<EsArticle> articleList) {
        // 1.创建索引和映射
        createIndexAndMapping();
        // 2.封装请求
        // 批量请求
        BulkRequest bulkRequest = new BulkRequest();
        for (EsArticle article : articleList) {
            IndexRequest request = new IndexRequest();
            request.index(EsConstant.ARTICLE_INDEX_NAME);
            request.source(JSON.toJSON(article), XContentType.JSON);
            bulkRequest.add(request);
        }
        try {
            restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RRException("文章上传失败");
        }
        // 操作RestHighLevelClient
        // TODO https://www.jianshu.com/p/21cf80a64eca   https://blog.csdn.net/qq_38677458/article/details/111219352
    }

    private void createIndexAndMapping() {
        GetIndexRequest request = new GetIndexRequest();
        request.indices();
        try {
            boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
            if (!exists) {
                CreateIndexRequest createIndexRequest = new CreateIndexRequest(EsConstant.ARTICLE_INDEX_NAME);
                createIndexRequest.mapping(EsConstant.ARTICLE_MAPPING, XContentType.JSON);
                restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public List<EsArticle> search(ArticleSearchRequest searchRequest) {
        // 关键词模糊查询

        return null;
    }

    @Override
    public void delete(Long[] ids) {

    }
}
